package com.fy56.platform.logistics.restful.api.controller.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.model.TbAdministrativeDivision;
import com.fy56.platform.logistics.service.wx.IAdministrativeDivisionService;
import com.fy56.platform.logistics.utils.HttpClientUtils;
import com.fy56.platform.logistics.utils.StaticUtil;
import com.google.common.collect.Maps;
import com.wordnik.swagger.annotations.ApiParam;
import com.zeus.base.privilege.model.TbCompany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 应用系统Controller
 *
 * @author li
 * @version 2018-04-02
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/administrativeDivision")
public class AdministrativeDivisionController {

    @Autowired
    IAdministrativeDivisionService iAdministrativeDivisionService;

    /**
     * 获取省份
     *
     * @return
     */
    @RequestMapping(value = "/getProvinceList", method = RequestMethod.POST)
    public ResponseEntity getProvinceList(){
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("administrativeDivisionLevel",0);
        List<TbAdministrativeDivision> list = iAdministrativeDivisionService.selectByParams(params);
        return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
    }

    /**
     * 根据省份获取城市列表
     *
     * @param provinceId
     * @return
     */
    @RequestMapping(value = "/getCityList", method = RequestMethod.POST)
    public ResponseEntity getProvinceList(
            @ApiParam(value = "用户名",name = "provinceId",required = true) @RequestParam String provinceId){
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("administrativeDivisionLevel",1);
        params.put("provinceId",provinceId);
        List<TbAdministrativeDivision> list = iAdministrativeDivisionService.selectByParams(params);
        return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
    }
    /**
     * 获取省份带全部
     *
     * @return
     */
    @RequestMapping(value = "/getProvinceAllList", method = RequestMethod.POST)
    public ResponseEntity getProvinceAllList(){
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("administrativeDivisionLevel",0);
        List<TbAdministrativeDivision> list = iAdministrativeDivisionService.selectByParams(params);
        List<TbAdministrativeDivision> alllist= new ArrayList<>();
//        TbAdministrativeDivision tbAdministrativeDivision = new TbAdministrativeDivision();
//        tbAdministrativeDivision.setAdministrativeDivisionCode("全部");
//        tbAdministrativeDivision.setAdministrativeDivisionName("全部");
//        tbAdministrativeDivision.setAdministrativeDivisionNameSpell("qb");
//        tbAdministrativeDivision.setFormatNames("全部");
//        tbAdministrativeDivision.setProvinceId(999999);
//        tbAdministrativeDivision.setAdministrativeDivisionId(999999);
//        alllist.add(tbAdministrativeDivision);
        if (list!=null){
            alllist.addAll(list);
        }
        return new ResponseEntity<>(ResultModel.ok(alllist), HttpStatus.OK);
    }

    /**
     * 根据省份获取城市列表带全部
     *
     * @param provinceId
     * @return
     */
    @RequestMapping(value = "/getCityAllList", method = RequestMethod.POST)
    public ResponseEntity getCityAllList(
            @ApiParam(value = "用户名",name = "provinceId",required = true) @RequestParam String provinceId){
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("administrativeDivisionLevel",1);
        params.put("provinceId",provinceId);
        List<TbAdministrativeDivision> list = new ArrayList<TbAdministrativeDivision>();
        if ("999999".equals(provinceId)){
            TbAdministrativeDivision tbAdministrativeDivision = new TbAdministrativeDivision();
            tbAdministrativeDivision.setAdministrativeDivisionCode("全部");
            tbAdministrativeDivision.setAdministrativeDivisionName("全部");
            tbAdministrativeDivision.setAdministrativeDivisionNameSpell("qb");
            tbAdministrativeDivision.setFormatNames("全部");
            tbAdministrativeDivision.setProvinceId(999999);
            list.add(tbAdministrativeDivision);
        }else{
            list = iAdministrativeDivisionService.selectByParams(params);
        }

        return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
    }

    /**
     * 获取认证中心区域数据列表
     *
     * @param provinceId
     * @return
     */
    @RequestMapping(value = "/getAdministrativeDivisionList", method = RequestMethod.POST)
    public ResponseEntity getAdministrativeDivisionList(
            @ApiParam(value = "查询数据等级 0省 1市 2区", name = "areaLevel",required=true) @RequestParam(required = true) Integer areaLevel,
            @ApiParam(value = "省编码", name = "provinceCode") @RequestParam(required = false) String provinceCode){
        List<HashMap<String, Object>> list = new ArrayList<>();
        try {
            Map<String, String> paramsNew = Maps.newHashMap();
            paramsNew.put("areaLevel", areaLevel.toString());
            paramsNew.put("parentCode", provinceCode);
            String result = HttpClientUtils.post(StaticUtil.restSearchURL + "/administrativedivision/queryChildList", paramsNew, null, 20000, 20000);
            if (result != null) {
                JSONObject j = JSON.parseObject(result);
                String object = j.get("object") + "";
                list = (List<HashMap<String, Object>>) JSONArray.parse(object);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
    }
}
